package com.rapig.mail.bean;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class MailAccount {

	private static String n_name = "name";
	private static String n_protocol = "protocol";
	private static String n_propAuthLogin = "propAuthLogin";
	private static String n_server = "server";
	private static String n_port = "port";
	private static String n_username = "username";
	private static String n_password = "password";
	private static String n_numUnread = "numUnread";
	private static String n_numNew = "numNew";
	private static String n_numTotal = "numTotal";

	private String name;
	private String protocol;
	private String propAuthLogin;
	private String server;
	private Integer port;
	private String username;
	private String password;
	private Integer numUnread;
	private Integer numNew;
	private Integer numTotal;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getPropAuthLogin() {
		return propAuthLogin;
	}

	public void setPropAuthLogin(String propAuthLogin) {
		this.propAuthLogin = propAuthLogin;
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getNumUnread() {
		return numUnread;
	}

	public void setNumUnread(Integer numUnread) {
		this.numUnread = numUnread;
	}

	public Integer getNumNew() {
		return numNew;
	}

	public void setNumNew(Integer numNew) {
		this.numNew = numNew;
	}

	public Integer getNumTotal() {
		return numTotal;
	}

	public void setNumTotal(Integer numTotal) {
		this.numTotal = numTotal;
	}

	public static JSONObject toJsonObject(MailAccount obj) {
		try {
			JSONObject jObj = new JSONObject();
			jObj.put(n_name, obj.getName());
			jObj.put(n_protocol, obj.getProtocol());
			jObj.put(n_propAuthLogin, obj.getPropAuthLogin());
			jObj.put(n_server, obj.getServer());
			jObj.put(n_port, obj.getPort());
			jObj.put(n_username, obj.getUsername());
			jObj.put(n_password, obj.getPassword());
			jObj.put(n_numUnread, obj.getNumUnread());
			jObj.put(n_numNew, obj.getNumNew());
			jObj.put(n_numTotal, obj.getNumTotal());
			return jObj;
		} catch (Exception ex) {
		}
		return null;
	}

	public static String toJsonList(List<MailAccount> list) {
		JSONArray array = new JSONArray();
		for (MailAccount obj : list) {
			array.put(toJsonObject(obj));
		}
		return array.toString();
	}

	public static String toJson(MailAccount obj) {
		JSONObject jObj = toJsonObject(obj);
		if (jObj != null) {
			return jObj.toString();
		}
		return null;
	}

	public static MailAccount fromJsonObject(JSONObject jObj) {
		MailAccount obj = null;
		try {
			obj = new MailAccount();
			if (!jObj.isNull(n_name)) {
				obj.setName(jObj.getString(n_name));
			}
			if (!jObj.isNull(n_protocol)) {
				obj.setProtocol(jObj.getString(n_protocol));
			}
			if (!jObj.isNull(n_propAuthLogin)) {
				obj.setPropAuthLogin(jObj.getString(n_propAuthLogin));
			}
			if (!jObj.isNull(n_server)) {
				obj.setServer(jObj.getString(n_server));
			}
			if (!jObj.isNull(n_port)) {
				obj.setPort(jObj.getInt(n_port));
			}
			if (!jObj.isNull(n_username)) {
				obj.setUsername(jObj.getString(n_username));
			}
			if (!jObj.isNull(n_password)) {
				obj.setPassword(jObj.getString(n_password));
			}
			if (!jObj.isNull(n_numUnread)) {
				obj.setNumUnread(jObj.getInt(n_numUnread));
			}
			if (!jObj.isNull(n_numNew)) {
				obj.setNumNew(jObj.getInt(n_numNew));
			}
			if (!jObj.isNull(n_numTotal)) {
				obj.setNumTotal(jObj.getInt(n_numTotal));
			}
		} catch (Exception ex) {
		}
		return obj;
	}

	public static List<MailAccount> fromJsonList(String json) {
		List<MailAccount> list = new ArrayList<MailAccount>();
		try {
			JSONArray array = new JSONArray(json);
			for (int i = 0; i < array.length(); i++) {
				JSONObject jObj = array.getJSONObject(i);
				if (jObj != null) {
					list.add(fromJsonObject(jObj));
				}
			}
		} catch (Exception ex) {
		}
		return list;
	}

	public static MailAccount fromJson(String json) {
		try {
			JSONObject jObj = new JSONObject(json);
			return fromJsonObject(jObj);
		} catch (Exception ex) {
		}
		return null;
	}

}
